/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pacificlms;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.sql.DriverManager;
import java.sql.Connection;
import java.util.ArrayList;
import pacificlms.Error.ErrorHandle;

/**
 * //fixme comment
 * @author Brian Hume
 */
public class ConnectionInfo {
    String HostUrl;
    String UserName;
    String Password;
    String Name;
    
    ConnectionInfo(String HostUrl, String UserName, String Password, String Name)
    {
        this.HostUrl = HostUrl;
        this.UserName = UserName;
        this.Password = Password;
        this.Name = Name;
        
    }
    
    static ArrayList<ConnectionInfo> LoadListFromURL(String URL)
    {
        ArrayList<ConnectionInfo> ConnectInfoList = new ArrayList();
        
        try
        {
            URL target = new URL(URL);
            BufferedReader in = new BufferedReader(
            new InputStreamReader(target.openStream()));
            
            String HostUrl, UserName, Password, Name;
            while ((HostUrl = in.readLine()) != null)
            {
                UserName = in.readLine();
                Password = in.readLine();
                Name = in.readLine();
                if (UserName == null || Password == null || Name == null)
                    throw(new Exception("Error parsing connection file"));
                ConnectionInfo CI = new ConnectionInfo(HostUrl, UserName, Password, Name);
                ConnectInfoList.add(CI);
            }
                
            in.close();
        }
        catch(Exception Ex)
        {
            ErrorHandle.Display("Connection Parse Error", "Connection list read error\n" + Ex.toString(), Ex, true);
        }
        return ConnectInfoList;
    }
    
    Connection CreateConnection() throws java.sql.SQLException
    {
        Connection Con = DriverManager.getConnection("jdbc:mysql:" + HostUrl, UserName, Password);
        return Con;
    }
    
    @Override
    public String toString()
    {
        return Name + "[" + HostUrl + "]";
    }
}
